//
//  KLCacheProxy.m
//  KoalaUtilKit
//
//  Created by Tommy on 15/1/13.
//  Copyright (c) 2015年 Koala Team. All rights reserved.
//

#import "KLCacheProxy.h"
#import "TMCache.h"
#import "SDImageCache.h"
#import "ReactiveCocoa.h"


@interface KLCacheProxy()


@end

@implementation KLCacheProxy

- (RACSignal*)objectForKey:(NSString*)key{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        id<NSCoding> object = [[TMDiskCache sharedCache]objectForKey:key];

        [subscriber sendNext:object];
        [subscriber sendCompleted];
        return nil;
    }];
}

- (RACSignal*)setObject:(id<NSCoding>)data forKey:(NSString*)key{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [[TMDiskCache sharedCache]setObject:data forKey:key];
        [subscriber sendNext:@(YES)];
        [subscriber sendCompleted];
        return nil;
    }];
}

- (RACSignal*)removeObjectForKey:(NSString*)key{
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [[TMDiskCache sharedCache] removeObjectForKey:key];
        [subscriber sendNext:@(YES)];
        [subscriber sendCompleted];
        return nil;
    }];
}

- (RACSignal*)clearAll{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [[TMDiskCache sharedCache] removeAllObjects:nil];
        [[SDImageCache sharedImageCache] clearDisk];
        [subscriber sendNext:@(YES)];
        [subscriber sendCompleted];
        return nil;
    }];
}
- (RACSignal*)clearExpired:(NSDate*)date{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [[TMDiskCache sharedCache]trimToDate:date];
        [[SDImageCache sharedImageCache] cleanDisk];
        [subscriber sendNext:@(YES)];
        [subscriber sendCompleted];
        return nil;
    }];
}
@end
